package com.jt.service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.EasyUITable;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemDescMapper itemDescMapper;
	
	/**
	 * 利用MP的方式实现分页查询
	 */
	@Override
	public EasyUITable findItemByPage(Integer page, Integer rows) {
		//1.page 定义分页相关数据
		Page<Item> iPage = new Page<>();
		iPage.setSize(rows);
		iPage.setCurrent(page);
		QueryWrapper<Item> queryWrapper = new QueryWrapper<Item>();
		queryWrapper.orderByDesc("updated");
		IPage<Item> itemPage = itemMapper.selectPage(iPage, queryWrapper);
		//2.获取分页数据
		int total = (int) itemPage.getTotal();
		List<Item> itemList = itemPage.getRecords();
		return new EasyUITable(total, itemList);
	}
	
	/**
	 * 2张表要求同时入库.
	 * 问题:itemDesc的入库需要依赖于item主键信息.item的主键入库之后才有.
	 * 思路1: 先item对象完成入库,之后根据item的属性查询数据库获取itemId  效率低
	 * 思路2: 能否在insert之后,将数据库的信息自动的封装到item对象中呢?
	 * 解决思路: 利用MP实现入库操作,之后动态回显item的信息.
	 */
	@Override
	@Transactional
	public void saveItem(Item item,ItemDesc itemDesc) {
		
		//item对象主键自增,当数据入库之后查询数据库中id的最大值+1实现入库操作
		item.setStatus(1)
			.setCreated(new Date())
			.setUpdated(item.getCreated());
		itemMapper.insert(item);
		
		itemDesc.setItemId(item.getId()) //获取itemId的值保证数据一致.
				.setCreated(item.getCreated())
				.setUpdated(item.getCreated());
		itemDescMapper.insert(itemDesc);
		
	}

	@Override
	public ItemDesc findItemDescById(Long itemId) {
		
		return itemDescMapper.selectById(itemId);
	}

	@Override
	@Transactional	//保证事物一致性
	public void updateItem(Item item, ItemDesc itemDesc) {
		Date date = new Date();
		item.setUpdated(date);
		itemMapper.updateById(item);
		
		//同时更新商品详情信息
		itemDesc.setItemId(item.getId())
				.setUpdated(date);
		itemDescMapper.updateById(itemDesc);
		
	}

	//实现批量删除
	@Override
	@Transactional
	public void deleteItems(Long[] ids) {
		
		List<Long> idList = Arrays.asList(ids);
		//itemMapper.deleteBatchIds(idList);
		itemDescMapper.deleteBatchIds(idList);
		
		//使用mapper xml形式实现删除操作
		itemMapper.deleteItems(ids);
	}

	/**
	 * 更新操作: 修改程序的状态信息
	 * sql: update tb_item set status=#{status},updated=#{updated}
	 * 		where id in (101,102,103);
	 */
	@Override
	public void updateStatus(Long[] ids, Integer status) {
		Item item = new Item();
		item.setStatus(status)
			.setUpdated(new Date());
		UpdateWrapper<Item> updateWrapper = new UpdateWrapper<Item>();
		updateWrapper.in("id", Arrays.asList(ids));
		itemMapper.update(item, updateWrapper);
	}

	@Override
	public Item findItemById(Long itemId) {
		return itemMapper.selectById(itemId);
	}
	
	/**
	 * 语法:SELECT * FROM tb_item LIMIT 起始位置,每页查询的条数
	 * 	第一页:每页20条
	 * 	  SELECT * FROM tb_item LIMIT 0,20  0-19
	 * 	第二页:
	 * 	  SELECT * FROM tb_item LIMIT 20,20  20-39
	 * 	第三页:
	 * 	  SELECT * FROM tb_item LIMIT 40,20  40-59
	 * 	第n页:
	 * 	  SELECT * FROM tb_item LIMIT (page-1)rows,rows
	 */
	/*@Override
	public EasyUITable findItemByPage(Integer page, Integer rows) {
		//1.动态的获取total记录 总数
		int total = itemMapper.selectCount(null);
		
		//2.动态获取数据库分页的结果
		int start = (page - 1)* rows;
		List<Item> itemList = itemMapper.findItemByPage(start,rows);
		return new EasyUITable(total, itemList);
	}*/
	
	
	
	
	
	
	
}
